package R.Dao;

import R.Entity.Diagnosis;
import R.Entity.Record;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class SickDao {
    public static Connection getConn() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hospital", "root", "123456");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }


    //数据新增
    public static int addS(Diagnosis r) {
        int sta = 0;
        try {
            Connection conn = RegDao.getConn();
            String sql = "insert into diagnosis(card_number,record_number,patient_name,patient_gender,doctor_number,doctor_name,doctor_title,drug_name,drug,drug_quantity,diagnosis_result,treatment_method,department_name,diagnosis_time) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);//预编译对象
            //赋值
            ps.setString(1, r.getCard_number());//就诊卡号
            ps.setString(2, r.getRecord_number());//病历编号
            ps.setString(3, r.getPatient_name());//病人姓名
            ps.setString(4, r.getPatient_gender());//病人性别
            ps.setString(5, r.getDoctor_number());//医生编号
            ps.setString(6, r.getDoctor_name());//医生姓名
            ps.setString(7, r.getDoctor_title());//医生职称
            ps.setString(8, r.getDrug_name());//药品名称
            ps.setString(9, r.getDrug());//药品规格
            ps.setString(10, r.getDrug_quantity());//药品数量
            ps.setString(11, r.getDiagnosis_result());//诊断结果
            ps.setString(12, r.getTreatment_method());//治疗方法
            ps.setString(13, r.getDepartment_name());//就诊科室
            ps.setString(14, r.getDiagnosis_time());//诊断时间
            sta = ps.executeUpdate();
            ps.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sta;
    }

    //显示全部数据
    public static List<Diagnosis> showAll() {
        List<Diagnosis> list = new ArrayList<Diagnosis>();
        try {
            Connection conn = RegDao.getConn();
            String sql = "select card_number,record_number,patient_name,patient_gender,doctor_number,doctor_name,doctor_title,drug_name,drug,drug_quantity,diagnosis_result,treatment_method,department_name,diagnosis_time from diagnosis where zt=1";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Diagnosis d = new Diagnosis();
                String card_number = rs.getString(1);//就诊卡号
                String record_number = rs.getString(2);//病历编号
                String patient_name = rs.getString(3);//病人姓名
                String patient_gender = rs.getString(4);//病人性别
                String doctor_number = rs.getString(5);//医生编号
                String doctor_name = rs.getString(6);//医生姓名
                String doctor_title = rs.getString(7);//医生职称
                String drug_name = rs.getString(8);//药品名称
                String drug = rs.getString(9);//药品规格
                String drug_quantity = rs.getString(10);//药品数量
                String diagnosis_result = rs.getString(11);//诊断结果
                String treatment_method = rs.getString(12);//治疗方法
                String department_name = rs.getString(13);//就诊科室
                String diagnosis_time = rs.getString(14);//诊断时间
                d.setCard_number(card_number);
                d.setRecord_number(record_number);
                d.setPatient_name(patient_name);
                d.setPatient_gender(patient_gender);
                d.setDoctor_number(doctor_number);
                d.setDoctor_name(doctor_name);
                d.setDoctor_title(doctor_title);
                d.setDrug_name(drug_name);
                d.setDrug(drug);
                d.setDrug_quantity(drug_quantity);
                d.setDiagnosis_result(diagnosis_result);
                d.setTreatment_method(treatment_method);
                d.setDepartment_name(department_name);
                d.setDiagnosis_time(diagnosis_time);
                list.add(d);
            }
            conn.close();
            ps.close();
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    //查询数据
    public static List<Diagnosis> ChaXun(Diagnosis r) {
        List<Diagnosis> list = new ArrayList<Diagnosis>();
        try {
            Connection conn = RegDao.getConn();
            String sql = "select card_number,record_number,patient_name,patient_gender,doctor_number,doctor_name,doctor_title,drug_name,drug,drug_quantity,diagnosis_result,treatment_method,department_name,diagnosis_time from diagnosis where zt=1 and card_number='" + r.getCard_number() + "' and record_number='" + r.getRecord_number() + "' and patient_name='" + r.getPatient_name() + "'";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Diagnosis d = new Diagnosis();
                String card_number = rs.getString(1);//就诊卡号
                String record_number = rs.getString(2);//病历编号
                String patient_name = rs.getString(3);//病人姓名
                String patient_gender = rs.getString(4);//病人性别
                String doctor_number = rs.getString(5);//医生编号
                String doctor_name = rs.getString(6);//医生姓名
                String doctor_title = rs.getString(7);//医生职称
                String drug_name = rs.getString(8);//药品名称
                String drug = rs.getString(9);//药品规格
                String drug_quantity = rs.getString(10);//药品数量
                String diagnosis_result = rs.getString(11);//诊断结果
                String treatment_method = rs.getString(12);//治疗方法
                String department_name = rs.getString(13);//就诊科室
                String diagnosis_time = rs.getString(14);//诊断时间
                d.setCard_number(card_number);
                d.setRecord_number(record_number);
                d.setPatient_name(patient_name);
                d.setPatient_gender(patient_gender);
                d.setDoctor_number(doctor_number);
                d.setDoctor_name(doctor_name);
                d.setDoctor_title(doctor_title);
                d.setDrug_name(drug_name);
                d.setDrug(drug);
                d.setDrug_quantity(drug_quantity);
                d.setDiagnosis_result(diagnosis_result);
                d.setTreatment_method(treatment_method);
                d.setDepartment_name(department_name);
                d.setDiagnosis_time(diagnosis_time);
                list.add(d);
            }
            conn.close();
            ps.close();
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    //返回实体类
    public static Diagnosis getD(Diagnosis a) {
        Diagnosis d = new Diagnosis();
        try {
            Connection conn = RegDao.getConn();
            String sql = "select card_number,record_number,patient_name,patient_gender,doctor_number,doctor_name,doctor_title,drug_name,drug,drug_quantity,diagnosis_result,treatment_method,department_name,diagnosis_time from diagnosis where zt=1 and card_number='" + a.getCard_number() + "' and record_number='" + a.getRecord_number() + "'";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                String card_number = rs.getString(1);//就诊卡号
                String record_number = rs.getString(2);//病历编号
                String patient_name = rs.getString(3);//病人姓名
                String patient_gender = rs.getString(4);//病人性别
                String doctor_number = rs.getString(5);//医生编号
                String doctor_name = rs.getString(6);//医生姓名
                String doctor_title = rs.getString(7);//医生职称
                String drug_name = rs.getString(8);//药品名称
                String drug = rs.getString(9);//药品规格
                String drug_quantity = rs.getString(10);//药品数量
                String diagnosis_result = rs.getString(11);//诊断结果
                String treatment_method = rs.getString(12);//治疗方法
                String department_name = rs.getString(13);//就诊科室
                String diagnosis_time = rs.getString(14);//诊断时间
                d.setCard_number(card_number);
                d.setRecord_number(record_number);
                d.setPatient_name(patient_name);
                d.setPatient_gender(patient_gender);
                d.setDoctor_number(doctor_number);
                d.setDoctor_name(doctor_name);
                d.setDoctor_title(doctor_title);
                d.setDrug_name(drug_name);
                d.setDrug(drug);
                d.setDrug_quantity(drug_quantity);
                d.setDiagnosis_result(diagnosis_result);
                d.setTreatment_method(treatment_method);
                d.setDepartment_name(department_name);
                d.setDiagnosis_time(diagnosis_time);
            }
            rs.close();
            ps.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return d;
    }

    //修改病人诊断信息
    public static int UpdateD(Diagnosis r) {
        int sta = 0;
        try {

            Connection conn = RegDao.getConn();
            String sql = "update diagnosis set card_number=?,record_number=?,patient_name=?,doctor_number=?,doctor_name=?,doctor_title=?,drug_name=?,drug_quantity=?,diagnosis_result=?,treatment_method=?,diagnosis_time=? where card_number='" + r.getCard_number() + "'";
            PreparedStatement ps = conn.prepareStatement(sql);//预编译对象
            //赋值
            ps.setString(1, r.getCard_number());//就诊卡号
            ps.setString(2, r.getRecord_number());//病历编号
            ps.setString(3, r.getPatient_name());//病人姓名
            ps.setString(4, r.getDoctor_number());//医生编号
            ps.setString(5, r.getDoctor_name());//医生姓名
            ps.setString(6, r.getDoctor_title());//医生职称
            ps.setString(7, r.getDrug_name());//药品名称
            ps.setString(8, r.getDrug_quantity());//药品数量
            ps.setString(9, r.getDiagnosis_result());//诊断结果
            ps.setString(10, r.getTreatment_method());//治疗方法
            ps.setString(11, r.getDiagnosis_time());//诊断时间
            sta = ps.executeUpdate();
            ps.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sta;
    }

    //删除（把状态改成0）
    public static int delD(Diagnosis r) {
        int sta = 0;
        try {

            Connection conn = RegDao.getConn();
            String sql = "update diagnosis set zt=0 where card_number='" + r.getCard_number() + "'";
            PreparedStatement ps = conn.prepareStatement(sql);//预编译对象
            //赋值
            sta = ps.executeUpdate();
            ps.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sta;
    }
}
